<?php

require_once "data/dao/GameDAO.php";
require_once "services/FacebookProxy.php";
require_once "www/_include/smarty/libs/Smarty.class.php";
require_once "utils/HTTPRequest.php";
require "conf.php";

class GamesRenderer{
	
	private $userId;
	private $gameDAO;
	private $smarty;
	private $locale = null;
	
	public function GamesRenderer(){
		
		$facebook = new FacebookProxy();
		$facebook->requireFrame();
		$this->userId = $facebook->requireLogin(); 
		
		
		$this->smarty = new Smarty();
		$this->gameDAO = new GameDAO();
		$this->locale = HTTPRequest::getParam("locale");
		
		switch(HTTPRequest::getParam("do")){
			case "alphabetical":
				$games = $this->gameDAO->getGamesAlphabetically(HTTPRequest::getParam("index"), $this->locale);
				break;
			case "search":
				$games = $this->gameDAO->getGamesByKeyword(HTTPRequest::getParam("keyword"), $this->locale);
				break;
			case "delist-game":
				if($this->userId != ADMIN_USER_ID){
					return;
				}
				$this->gameDAO->delistGame(HTTPRequest::getParam("game-id"));
				break;
			default:
				break;
		}
		
		$this->getPlaceholderValues($games);
		$this->renderPage();
	}
	
	private function getPlaceholderValues($games){
		
		$this->smarty->assign("indexedGames", $games);
		$this->smarty->assign("alphabet", array("A", "B", "C", "D", "E", "F", "G",
			"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z"));
		$this->smarty->assign("styleSheet", file_get_contents("html/wiiSocial.css"));
		$this->smarty->assign("javaScript", file_get_contents("html/wiiSocial.js"));
		$this->smarty->assign("HOST", HOST_URL);
		$this->smarty->assign("numberOfGames", $this->getNumberOfGames());
		$this->smarty->assign("activeIndex", $_REQUEST['index']);
		$this->smarty->assign("numberOfMatchingResults", count($games));
		
		$this->smarty->assign("adminUser",(ADMIN_USER_ID == (string) $this->userId));
			
	}
	
	private function renderPage(){
		
		$this->smarty->display("games.tpl");
	}
	
	private function getNumberOfGames(){
		$gdao = new GameDAO();
		return $gdao->getTotalGames();
	}
}
$gamesRenderer = new GamesRenderer();
?>